package com.asap.demo.ck.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * 
 * @author ww
 *
 */
public final class ConfigCenter {

	private static final Logger logger = LoggerFactory.getLogger(ConfigCenter.class);

	private Config config = null;
    private static String SYSTEM_FILE_SEPARATOR = File.separator;
    String configPath = "conf" + SYSTEM_FILE_SEPARATOR + "dpEvent-info.properties";
	private ConfigCenter() {		
		this.config = new DefaultConfig(this.getProperty());
	}
	
	private ConfigCenter(Map<String, String> map) {		
		this.config = new DefaultConfig(map);
	}
	
	private static ConfigCenter center = null;
	
	/**
	 * @Title: getInstance   
	 * @Description: TODO  
	 * @param propertys 配置map
	 * @return ConfigCenter  
	 * @throws
	 */
	public static synchronized ConfigCenter getInstance(Map<String, String> propertys) {
		if (center == null) {
			center = new ConfigCenter(propertys);
		}
		return center;
	}
	
	/**
	 * @Title: getInstance   
	 * @Description: TODO 获取单例类
	 * @return ConfigCenter  
	 * @throws
	 */
	public static synchronized ConfigCenter getInstance() {
		if (center == null) {
			center = new ConfigCenter();
		}
		return center;
	}
	
	public Config getConfig() {
		
		return this.config;
	}
	
	/**
	 * @Title: getProperty   
	 * @Description: TODO  
	 * @return Map<String,String>   
	 * @throws
	 */
	private Map<String, String> getProperty() {
		Properties prop = new Properties();
		try {
//			Class<?> clazz = this.getClass();
//			prop.load(clazz.getResourceAsStream("conf/cfg.properties"));
//			
			File file = new File(configPath);
            logger.info("连接配置文件的路径为:{}", configPath);
			FileInputStream fis = new FileInputStream(file);
			prop.load(fis);
			logger.info("prop:"+prop.size());
		} catch (IOException e) {
			logger.error("Load properties error: ",e);
		}
		HashMap<String, String> map = new HashMap<>();
		for(Entry<Object, Object> entry : prop.entrySet()) {
			map.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
		}
		logger.info("Load properties: "+prop.toString());
		return map;
	}
	
	/**
	 * @ClassName: DefaultConfig   
	 * @Description: TODO  
	 * @author: suyb@asiainfo-sec.com  
	 * @date:2018年4月19日 上午11:02:52
	 */
	private class DefaultConfig implements Config {

		private Map<String, String> configMap;
		
		DefaultConfig(Map<String, String> configMap) {
			logger.info("the config size is : " + configMap.size());
			if(configMap!= null && configMap.size() != 0) {
				for(Entry<String, String> entry : configMap.entrySet()) {
					logger.debug("the key of the configMap is " + entry.getKey() + "\n" 
							+ "the value of the configMap is " + entry.getValue());
				}
			}
			this.configMap = configMap;
		}
		
		@Override
		public String getItem(String name) {
			
			return this.configMap.get(name);
		}

		@Override
		public Map<String, String> getAllItem() {

			return this.configMap;
		}
		
	}
}
